const fs = require('fs');
const { type } = require('os');
const path = require('path')
class HomeCtrol {
  index(ctx) {
    ctx.body = '<h1>这是主页</h1>';
  }
  async upload(ctx) {
    const file = ctx.request.files.file;
    console.log('file:', file)
    const basename = path.basename(file.path);
    console.log(`${ctx.origin}/uploads/${basename}`)
    ctx.body = {
      code: 0,
      url: `${ctx.origin}/uploads/${basename}`
    };
  }
  async actionUpload(ctx) {
    const file = ctx.request.body;
    const data = Buffer.from(String(file), 'base64')
    console.log('data:', data)
    fs.writeFileSync(`./public/uploads/${Date.now()}.png`, data)
    ctx.body = {
      code: 0,
      msg: 'success'
    }
  }

  async login(ctx) {
    console.log('我是后台登陆接口')
    ctx.body = {
      code: 0,
      msg: 'success',
      data: { token: 'login_1803B_11_12'}
    }
  }
  
  async userInfo(ctx) {
    ctx.body = {
      code: 0,
      msg: 'success',
      data: {
        avatar: 'http://localhost:3000/images/title1.jpeg',
        name: 'devin',
        sex: 1,
        age: 1,
        userId: 1
      }
    }
  }
}
// router.get('/api/userInfo', (req, res) => {
//   // 判断token符合不符合，如果符合才将用户信息传递出去
//   res.send({
//     code: 0,
//     msg: 'success',
//     data: {
//       avatar: 'http://localhost:3000/images/title1.jpeg',
//       name: 'devin',
//       sex: 1,
//       age: 1 
//     }
//   })
// })


// router.post('/api/login', (req, res) => {
//   res.status(200).send({
//     code: 0,
//     msg: 'success',
//     data: {token: 'login_1803B_11_12'}
//   })
// }) 
module.exports = new HomeCtrol()
